package com.mcptt.notify.xcap;

import org.jdom.Element;
import org.jdom.JDOMException;

import com.cloudptt.api.product.log.Log;
import com.mcptt.config.group.Group;
import com.mcptt.config.service.Service;
import com.mcptt.config.ue.config.UeConfig;
import com.mcptt.config.ue.initial.UeInitProfile;
import com.mcptt.config.user.profile.UserProfile;
import com.mcptt.db.model.UserInfo;

public class XcapDoc
{
	public static final String Name = "document";
	private static final String Name_New_Etag = "new-etag";
	private static final String Name_Previous_Etag = "previous-etag";
	private static final String Name_Sel = "sel";
	private String newEtag = null;
	private String previousEtag = null;
	private String sel = null;
	private String fileName = UeInitProfile.FileName;
	private String groupName = "";

	public static XcapDoc getUeInitProfileXcapDoc(String mcId)
	{
		XcapDoc doc = new XcapDoc();
		doc.newEtag = "";
		doc.previousEtag = "";
		doc.sel = "/org.3gpp.mcptt.ue-init-config/users/" + mcId + "/index";
		doc.fileName = UeInitProfile.FileName;
		doc.groupName = "";

		return doc;
	}

	public void parse(Element elem) throws JDOMException
	{
		if(elem == null)
		{
			return;
		}
		this.newEtag = elem.getAttributeValue(Name_New_Etag);
		this.previousEtag = elem.getAttributeValue(Name_Previous_Etag);
		this.sel = elem.getAttributeValue(Name_Sel);
		this.fileName = getFileTypeByFilePath(sel);
		Log.debug(this.getClass(),"parse fileName = " + fileName);
		if(this.fileName!= null
				&& (Group.FileName_Mcptt.equals(fileName)
						|| Group.FileName_Mcvideo.equals(fileName)
						|| Group.FileName_Mcdata.equals(fileName)))
		{
			groupName = getGroupNameByFilePath(sel);
		}
		Log.debug(this.getClass(),"parse 2 fileName = " + fileName);
	}

	public String getSel()
	{
		return sel;
	}
	public String getNewEtag()
	{
		return newEtag;
	}
	public String getFileName()
	{
		return fileName;
	}

	public String getGroupName()
	{
		return groupName;
	}
	private String getFileTypeByFilePath(String filePath)
	{
		Log.debug(getClass(), "getFileTypeByFilePath filePath = " + filePath);
		if(filePath == null
				|| filePath.length() <= 0)
		{
			return UeInitProfile.FileName;
		}
		if(filePath.contains("org.3gpp.mcptt.ue-init-config"))
		{
			return UeInitProfile.FileName;
		}
		else if(filePath.contains("org.3gpp.mcptt.ue-config"))
		{
			return UeConfig.FileName;
		}
		else if(filePath.contains("org.3gpp.mcptt.user-profile"))
		{
			return UserProfile.FileName_Mcptt;
		}
		else if(filePath.contains("org.3gpp.mcvideo.user-profile"))
		{
			return UserProfile.FileName_Mcvideo;
		}
		else if(filePath.contains("org.3gpp.mcdata.user-profile"))
		{
			return UserProfile.FileName_Mcdata;
		}
		else if(filePath.contains("org.3gpp.mcptt.service-config"))
		{
			return Service.FileName;
		}
		else if(filePath.contains("org.openmobilealliance.groups"))
		{
			return Group.FileName_Mcptt;
		}
		else if(filePath.contains("mcvideo-groupinfo.xml"))
		{
			return Group.FileName_Mcvideo;
		}
		else if(filePath.contains("mcdata-groupinfo.xml"))
		{
			return Group.FileName_Mcdata;
		}
		return null;
//		return UeInitProfile.FileName;
	}

	//    private String getGroupNameByFilePath(String filePath)
	//    {
	//	int index = filePath.lastIndexOf("/");
	//	int size = filePath.length();
	//	if(index > 0
	//		&& index < size)
	//	{
	//	    String groupName = filePath.substring(index+1,size);
	//	    return groupName;
	//	}
	//	return "";
	//    }

	private static String Key = "global/byGroupID/";
	private String getGroupNameByFilePath(String filePath)
	{
		Log.debug(getClass(), "getGroupNameByFilePath filePath = " + filePath);
		
		int beginIndex = filePath.indexOf(Key);
		beginIndex = beginIndex + Key.length();
		int endIndex = filePath.length();
		Log.debug(getClass(), "getGroupNameByFilePath beginIndex = " + beginIndex);
		Log.debug(getClass(), "getGroupNameByFilePath endIndex = " + endIndex);
		int size = filePath.length();
		if(beginIndex >0
				&& beginIndex < size
				&& endIndex > 0
				&& endIndex < size)
		{
			String groupName = filePath.substring(beginIndex,endIndex);
			Log.debug(getClass(), "getGroupNameByFilePath groupName = " + groupName);
			return groupName;
		}
		return "";
	}    

	public String toString()
	{
		StringBuffer strBuf = new StringBuffer("\r\n");
		strBuf.append("newEtag = ").append(newEtag).append(" ");
		strBuf.append("sel = ").append(sel).append(" ");
//		strBuf.append("<").append(Name).append(" ");
//		strBuf.append(Name_New_Etag).append("=\"").append(newEtag).append("\" ");
//		strBuf.append(Name_Sel).append("=\"").append(sel).append("\" ");
//		strBuf.append("/>");

		return strBuf.toString();
	}
}
